﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

public partial class MyUser : System.Web.UI.Page
{
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.IsAuthenticated == false)
        {
            Response.Redirect("login.aspx");
        }

        if (Request.IsAuthenticated)
        {
            string n = "";
            SqlCommand sCmd = new SqlCommand("SELECT name FROM USERS WHERE id=@id", conn);
            sCmd.Parameters.Add(new SqlParameter("@id", SqlDbType.Int)).Value = User.Identity.Name;
            conn.Open();
            SqlDataReader dr = sCmd.ExecuteReader();
            while (dr.Read())
            {
                string name = Convert.ToString(dr[0].ToString());
                n = name;
            }
            dr.Close();
            conn.Close();

            WelcomeBackMessage.Text = "Welcome back, " + n + "!";

            AuthenticatedHeaderPanel.Visible = true;
            AuthenticatedMessagePanel.Visible = true;
            AuthenticatedMenuPanel.Visible = true;

            if (!IsPostBack)
            {
                SetData();
            }
        }
        else
        {
            AuthenticatedHeaderPanel.Visible = false;
            AuthenticatedMessagePanel.Visible = false;
            AuthenticatedMenuPanel.Visible = false;
        }
    }
    
    private void SetData()
    {
        conn.Open();
        SqlCommand selCmd = new SqlCommand("SELECT type_licence FROM licence_type", conn);
        SqlDataReader rd = selCmd.ExecuteReader();

        while (rd.Read())
        {
            DropLicence.Items.Add(rd["type_licence"].ToString());
        }
        rd.Close();
        conn.Close();

        Response.Write(ID.Text = Session["User_ID"].ToString());
        int usID = Convert.ToInt32(ID.Text);
        Response.Write(admin.Text = Session["admin"].ToString());

        tb_name.Text = _User.EditUser(usID)._Name;
        tb_secondname.Text = _User.EditUser(usID)._SecondName;
        tb_login.Text = _User.EditUser(usID)._Login;
        tb_password.Text = "";
        tb_email.Text = _User.EditUser(usID)._Email;
        string id_lic = _User.EditUser(usID)._ID_Licence.ToString();

        conn.Open();
        SqlCommand selCmd1 = new SqlCommand("SELECT type_licence FROM licence_type WHERE id=@id", conn);
        selCmd1.Parameters.Add(new SqlParameter("@id", SqlDbType.NVarChar)).Value = id_lic;
        SqlDataReader rd1 = selCmd1.ExecuteReader();

        while (rd1.Read())
        {
            DropLicence.Items.Add(rd1["type_licence"].ToString());
        }
        rd1.Close();
        conn.Close();

        check_superuser.Checked = _User.EditUser(usID)._Superuser;
        check_activated.Checked = _User.EditUser(usID)._Activated;
        check_additional_user.Checked = _User.EditUser(usID)._Additional_user;

        Response.Clear();
    }
    public string MD5String(string input)
    {
        MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
        byte[] inputBytes = Encoding.ASCII.GetBytes(input);
        byte[] hashBytes = md5.ComputeHash(inputBytes);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < hashBytes.Length; i++)
        {
            sb.Append(hashBytes[i].ToString("x2"));
        }
        return sb.ToString();
    }
    protected void UpdateButton_Click(object sender, EventArgs e)
    {
        int id_lic = 0;
        conn.Open();
        SqlCommand selCmd = new SqlCommand("SELECT id FROM licence_type WHERE type_licence=@type_licence", conn);
        selCmd.Parameters.Add(new SqlParameter("@type_licence", SqlDbType.NVarChar)).Value = DropLicence.SelectedValue;
        SqlDataReader rd = selCmd.ExecuteReader();
        while (rd.Read())
        {
            id_lic = Convert.ToInt32(rd["id"].ToString());
        }
        rd.Close();
        conn.Close();

        string id = ID.Text;
        string name = tb_name.Text;
        string secondname = tb_secondname.Text;
        string login = tb_login.Text;
        string password = tb_password.Text;
        string emil = tb_email.Text;
        string id_licence = id_lic.ToString();
        string supeuser = check_superuser.Checked.ToString();
        string activated = check_activated.Checked.ToString();
        string additional_user = check_additional_user.Checked.ToString();

        if (admin.Text == "true")
        {
            _User.UpdateME(id, name, secondname, login, MD5String(password), emil, id_licence, supeuser, activated, additional_user);
        }
        else
        {
            _User.Update(id, name, secondname, login, MD5String(password), emil, id_licence, supeuser, activated, additional_user, User.Identity.Name);
        }
        Response.Redirect("MyUsers.aspx");
    }

    protected void CancelButton_Click(object sender, EventArgs e)
    {
        Response.Redirect("MyUsers.aspx");
    }
}